/*
https://leetcode-cn.com/problems/first-missing-positive/solution/que-shi-de-di-yi-ge-zheng-shu-by-leetcode-solution/
 */
public class Solution41 {
    public int firstMissingPositive(int[] nums) {
        int n=nums.length;
        for (int i=0;i< nums.length;i++){
            while (nums[i]>0 && nums[i]<=n && nums[nums[i]-1]!=nums[i]){
                int temp=nums[nums[i]-1];
                nums[nums[i]-1]=nums[i];
                nums[i]=temp;
            }
        }
        for (int i=0;i<n;i++){
            if (nums[i]!=i+1){
                return i+1;
            }
        }
        return n+1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution41().firstMissingPositive(new int[]{1,2,0}));
    }
}
